//
//  TTRefresh.m
//  TTKit
//
//  Created by Apple on 2017/12/21.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "TTRefresh.h"
#import "TTBundle.h"
#import "UIKit+TTKit.h"

CGRect kZZZLogoViewBounds = {0,0,40,40};
@implementation TTRefresh
#pragma comment 懒加载
- (UIImageView *)circleView{
    if (!_circleView) {
        _circleView = [[UIImageView alloc] initWithImage:[TTBundle tt_refreshImage]];
    }
    return _circleView;
}
- (UIImageView *)logoView{
    if (!_logoView) {
        _logoView = [[UIImageView alloc] init];
    }
    return _logoView;
}
- (CALayer *)circleLayer{
    if (!_circleLayer) {
        _circleLayer = [self getShape];
    }
    return _circleLayer;
}

#pragma comment 动画
-(CABasicAnimation *)getTransformAnimation{
    CABasicAnimation *animation   = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    //指定对transform.rotation属性做动画
    animation.duration            = 1.0f; //设定动画持续时间
    animation.byValue             = @(M_PI*2); //设定旋转角度，单位是弧度
    animation.fillMode            = kCAFillModeForwards;//设定动画结束后，不恢复初始状态之设置一
    animation.repeatCount         = 1000;//设定动画执行次数
    animation.removedOnCompletion = NO;//设定动画结束后，不恢复初始状态之设置二
    return animation;
}
//圆的绘制进度 0~
-(CAShapeLayer *)getShape{
    CGRect rect = CGRectMake(2.5, 2.5,_circleView.bounds.size.width-6, _circleView.bounds.size.height-6);
    UIBezierPath *path       = [UIBezierPath bezierPathWithOvalInRect:rect];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillColor     = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor   = RGBCOLOR(51,153,255,1.0f).CGColor;
    shapeLayer.lineWidth     = 2.0f;
    shapeLayer.path          = path.CGPath;
    shapeLayer.frame         = rect;
    shapeLayer.strokeStart   = 0;
    shapeLayer.strokeEnd     = 0;
    return shapeLayer;
}
@end





@interface TTRefreshHeader()
@property (nonatomic,strong)TTRefresh *ttRefresh;
@end

@implementation TTRefreshHeader
#pragma mark - 构造方法
+ (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock
{
    TTRefreshHeader *cmp = [[self alloc] init];
    cmp.refreshingBlock = refreshingBlock;
    return cmp;
}
+ (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action
{
    TTRefreshHeader *cmp = [[self alloc] init];
    [cmp setRefreshingTarget:target refreshingAction:action];
    return cmp;
}

#pragma mark - 覆盖父类的方法

#pragma mark 初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
}
#pragma mark 设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    if(_ttRefresh == nil){
        self.ttRefresh = [[TTRefresh alloc]init];
        [_ttRefresh.logoView addSubview:_ttRefresh.circleView];
        [_ttRefresh.logoView.layer addSublayer:_ttRefresh.circleLayer];
        [self.scrollView addSubview:_ttRefresh.logoView];
        _ttRefresh.circleView.hidden = YES;
    }
    
    _ttRefresh.logoView.center = CGPointMake(self.mj_w/2.0, self.mj_h/2.0 - MJRefreshHeaderHeight);
    _ttRefresh.logoView.bounds = kZZZLogoViewBounds;
    _ttRefresh.circleView.frame = _ttRefresh.logoView.bounds;
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    //根据拖曳度更新动画
    _ttRefresh.circleLayer.strokeEnd = self.pullingPercent > 0 ? self.pullingPercent:0;
    
    
    //直接图片翻转
    //if(self.pullingPercent > 0 && MJRefreshStateIdle == self.state)
    //    self.circleView.transform = CGAffineTransformMakeRotation(M_PI/self.pullingPercent);
}
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            _ttRefresh.circleView.hidden = YES;
            _ttRefresh.circleLayer.hidden = NO;
            _ttRefresh.circleLayer.strokeEnd = 0;
            break;
        case MJRefreshStatePulling:
            break;
        case MJRefreshStateRefreshing:
            _ttRefresh.circleView.hidden = NO;
            _ttRefresh.circleLayer.hidden = YES;
            [_ttRefresh.circleView.layer addAnimation:[_ttRefresh getTransformAnimation] forKey:nil];
            break;
        default:
            break;
    }
}
- (void)endRefreshing
{
    [super endRefreshing];
    _ttRefresh.circleView.hidden = YES;
    _ttRefresh.circleLayer.hidden = YES;
    [_ttRefresh.circleView.layer removeAllAnimations];
}
@end









@interface TTRefreshFooter()
@property (nonatomic,strong)TTRefresh *ttRefresh;
@end

@implementation TTRefreshFooter
#pragma mark - 构造方法
+ (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock
{
    TTRefreshFooter *cmp = [[self alloc] init];
    cmp.refreshingBlock = refreshingBlock;
    return cmp;
}
+ (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action
{
    TTRefreshFooter *cmp = [[self alloc] init];
    [cmp setRefreshingTarget:target refreshingAction:action];
    return cmp;
}

#pragma mark - 覆盖父类的方法

#pragma mark 初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
}
#pragma mark 设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    if(_ttRefresh == nil){
        self.ttRefresh = [[TTRefresh alloc]init];
        [_ttRefresh.logoView addSubview:_ttRefresh.circleView];
        [_ttRefresh.logoView.layer addSublayer:_ttRefresh.circleLayer];
        [self.scrollView addSubview:_ttRefresh.logoView];
        _ttRefresh.circleView.hidden = YES;
    }
    
    _ttRefresh.logoView.frame = CGRectMake(self.mj_w/2.0-kZZZLogoViewBounds.size.width/2,
                                           self.scrollView.contentSize.height+4,
                                           kZZZLogoViewBounds.size.width, kZZZLogoViewBounds.size.height);
    _ttRefresh.circleView.frame = _ttRefresh.logoView.bounds;
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    //根据拖曳度更新动画
    //_ttRefresh.circleLayer.strokeEnd = self.pullingPercent > 0 ? self.pullingPercent:0;
}
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            _ttRefresh.circleView.hidden = YES;
            _ttRefresh.circleLayer.hidden = NO;
            break;
        case MJRefreshStatePulling:
            break;
        case MJRefreshStateRefreshing:
            _ttRefresh.circleView.hidden = NO;
            _ttRefresh.circleLayer.hidden = YES;
            [_ttRefresh.circleView.layer addAnimation:[_ttRefresh getTransformAnimation] forKey:nil];
            break;
        default:
            break;
    }
}
- (void)endRefreshing
{
    self.state = MJRefreshStateIdle;
    _ttRefresh.circleView.hidden = YES;
    _ttRefresh.circleLayer.hidden = YES;
    _ttRefresh.circleLayer.strokeEnd = 0;
    [_ttRefresh.circleView.layer removeAllAnimations];
}
- (void)endRefreshingWithNoMoreData
{
    [self endRefreshing];
    [super endRefreshingWithNoMoreData];
}

- (void)resetNoMoreData
{
    [self endRefreshing];
    [super resetNoMoreData];
}
@end

